import { DebounceSettings, DebouncedFunc, debounce } from "lodash";
import { useRef, useMemo, useEffect } from "react";


export function useDebounce<T extends (...args: any) => any>(func: T, wait: number, deps: React.DependencyList, options?: DebounceSettings): DebouncedFunc<T> {
  const ref_fn = useRef(func);
  ref_fn.current = func;
  const debounced = useMemo(() => debounce((...args: any[]) => ref_fn.current(...args), wait, options), deps);
  const ref_debounced = useRef(debounced);
  ref_debounced.current = debounced;
  useEffect(() => () => ref_debounced.current.cancel(), []);
  return debounced;
}
